package com.key.w8.http1.connection;

import com.key.w8.common.ConnectionAdapter;
import com.key.w8.common.execute.ChainExecuteHandler;
import com.key.w8.http1.entity.HttpRequest;
import com.key.w8.http1.handler.codec.Http1Codec;
import com.key.w8.http1.invoker.Http1ReaderInvoker;
import com.key.w8.http1.invoker.Http1WriterInvoker;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.ChannelPromise;

import java.io.IOException;


/**
 * Connection基本承接读写功能，取决于*客户端*是否使用连接池管理所有连接
 * 具体的数据读写仍然是交由pipeline中的handler执行，如{@link Http1Codec}
 * 此外，继承写能力{@link Http1WriterInvoker}
 * 以及读时回调能力{@link Http1ReaderInvoker}
 * 连接关闭时一定要检测是否已经关闭了
 */
public interface Connection extends ConnectionAdapter,Http1ReaderInvoker,Http1WriterInvoker {
    interface ErrorListener {
        void error(Throwable e);
    }

    interface CloseListener{
        void onClose(Connection connection);
    }

    interface CreatedListener{
        void onCreated(Connection connection);
    }
    interface WriteRequestListener{
        void preWriteRequest(HttpRequest httpRequest);
    }
    Connection addErrorListener(ErrorListener errorListener);
    Connection addCloseListener(CloseListener closeListener);
    Connection addCreatedListener(CreatedListener createdListener);
    Connection addChainExecuteHandler(ChainExecuteHandler handler);
    Connection addChainErrorHandler(ChainExecuteHandler handler);
    void shutDown() throws IOException;
    ChannelPromise getPromise();
    //为支持重试机制的subClass预留接口
    void tryClose() throws IOException;
    ByteBufAllocator alloc();

    void addWriteRequestListener(WriteRequestListener writeRequestListener);

}
